// 常用的全局方法
const Common = {
	install(app) {
		app.config.globalProperties.navigateTo = function() {
			return (url) => {
				uni.navigateTo({
					url: url
				})
			};
		}()
		app.config.globalProperties.switchTab = function() {
			return (url) => {
				uni.switchTab({
					url: url
				})
			};
		}()
		app.config.globalProperties.navigateBack = function() {
			return (delta = 1) => {
				uni.navigateBack({
					delta: delta
				})
			};
		}()
		// 计算两个经纬度的距离
		app.config.globalProperties.calculateDistance = function() {
			//lat1=我的纬度, lon1=我的经度, lat2=目标纬度, lon2=目标经度
			return (lat1, lon1, lat2, lon2) => {
				let deg2rad = (deg) => {
					return deg * (Math.PI / 180)
				}
				// console.log('lat1, lon1, lat2, lon2', lat1, lon1, lat2, lon2)
				let R = 6371; //地球半径
				let dLat = deg2rad(lat2 - lat1)
				let dLon = deg2rad(lon2 - lon1)
				let a =
					Math.sin(dLat / 2) * Math.sin(dLat / 2) +
					Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
					Math.sin(dLon / 2) * Math.sin(dLon / 2);
				let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
				let d = R * c; // 距离，单位：千米  
				let e = parseFloat(d)
				let f = e.toFixed(2)
				return f;
			};
		}()
	},
};

export default Common;